package org.example.sale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @className: AgentInvocationHandler
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/3/12 10:43
 */
public class AgentInvocationHandler implements InvocationHandler {
    private static Logger logger = LoggerFactory.getLogger(AgentInvocationHandler.class);
    private Object factory;  // 真实对象（支持任意接口实现类）
    BeforeAfter beforeAfter ;
    public AgentInvocationHandler(Object factory , BeforeAfter beforeAfter) {
        this.factory = factory;
        this.beforeAfter = beforeAfter ;
    }

    public void setBeforeAfter(BeforeAfter beforeAfter) {
        this.beforeAfter = beforeAfter;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        beforeAfter.before();
        Object result = method.invoke(factory, args);
        beforeAfter.after();
        return result;
    }
}
